﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="CssEditionController.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Portals.MVC4Portal.Models.JSonModels;
using InnovaApp.Portals.MVC4Portal.Models.WebAPIModels.JSTO;
using INNOVACALL.Framework.Common;
using INNOVACALL.Framework.Common.Extensions;
using INNOVACALL.Framework.Common.MVCHelpers;

namespace InnovaApp.Portals.MVC4Portal.Controllers.WebAPIControllers.CssManagement
{
    /// <summary>
    /// Class CssEditionController
    /// </summary>
    [Authorize(Roles = "Admin")]
    public class CssEditionController : _WebAPIControllers_Base<CssEdition_WebAPIModel>
    {
        /// <summary>
        /// The e method base
        /// </summary>
        protected static string eMethodBase = "InnovaApp.Portals.MVC4Portal.Controllers.WebAPIControllers.CssEditionController";

        /// <summary>
        /// Gets the specified language.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <returns>HttpResponseMessage.</returns>
        public HttpResponseMessage Get(string language)
        {
            HttpResponseMessage toReturn;
            string eMethod = eMethodBase + "Get";
            object eParameters = new { language = language };
            try
            {
                string cssBasePath = HttpContext.Current.Server.MapPath("/Content/EditableContent/base.css");
                base.ApiData.cssContent = cssBasePath.ReadContentFile();
                base.ApiData.success = true;
                toReturn = base.CreateResponse(HttpStatusCode.OK);
            } catch (Exception ex)
            {
                base.ApiData.success = false;
                base.ApiData.message = ex.Message;
                base.ApiData.iFrameContent = ex.StackTrace;
                base.ApiData.scriptToExecute = StringEnum.GetStringValue(enScriptToExecute.DisplayMessage);
                toReturn = base.CreateResponse(HttpStatusCode.OK);
            }
            return toReturn;
        }

        /// <summary>
        /// Posts the specified language.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <param name="model">The model.</param>
        /// <returns>HttpResponseMessage.</returns>
        public HttpResponseMessage Post(string language, SaveCss_JSonModel model)
        {
            HttpResponseMessage toReturn;
            string eMethod = eMethodBase + "Post";
            object eParameters = new { model = model.cssContent};
            string cssBasePath = HttpContext.Current.Server.MapPath(PortalEnvironment.CssBasePath);
            FileAttributes attributes = System.IO.File.GetAttributes(cssBasePath);
            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                System.IO.File.SetAttributes(cssBasePath, attributes ^ FileAttributes.ReadOnly);
            try
            {
                System.IO.File.WriteAllText(cssBasePath, model.cssContent);
                base.ApiData.success = true;
                base.ApiData.message = Resources.View_CSSEditor_Saved;
                toReturn = base.CreateResponse(HttpStatusCode.OK);
            } catch (Exception ex)
            {
                base.ApiData.success = false;
                base.ApiData.message = ex.Message;
                base.ApiData.iFrameContent = ex.StackTrace;
                base.ApiData.scriptToExecute = StringEnum.GetStringValue(enScriptToExecute.DisplayMessage);
                toReturn = base.CreateResponse(HttpStatusCode.OK);
            }
            return toReturn;
        }
    }
}
